# 将目录下面的图片重命名
# 将图片后缀改成png
import os

def rename_images(directory):
    # 遍历目录中的所有文件
    list = []
    index = 1
    for filename in os.listdir(directory):
        # 检查文件是否是图片
        if filename.endswith(('.png', '.jpeg')):
            # 构建新的文件名
            new_filename = filename.replace(' ', '_')  # 将空格替换为下划线
            new_filename = new_filename.replace('.png', '.jpg')
            # 重命名文件
            os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
            print(f"重命名: {filename} -> {new_filename}")  
        # 将文件名和排序保存在list中
        list.append({
            "cardName": filename.replace('.jpg', ''),
            "order": index,
            "cardType": "古埃及神图塔罗",
            "owerer":""
        })
        index += 1
    print(list)
    # 将list写入csv
    import csv


    # 写入CSV文件
    csv_filename = 'cards.csv'
    with open(csv_filename, 'w', newline='') as csvfile:
        fieldnames = ['cardName', 'order', 'cardType', 'owerer']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        for row in list:
            writer.writerow(row)
        
if __name__ == "__main__":
    directory = "./古埃及神图塔罗/"
    rename_images(directory)
